<template>
  <el-dialog :title="info.title" :visible.sync="info.isShow" @opened="opened">
    <el-form :model="form">
      <el-form-item label="一级分类" :label-width="formLabelWidth">
        <el-select
          v-model="form.first_cateid"
          placeholder="--请选择-"
          @change="changeCate"
        >
          <!-- 遍历分类名称 -->
          <el-option
            v-for="item in cates"
            :key="item.id"
            :label="item.catename"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="二级分类" :label-width="formLabelWidth">
        <el-select v-model="form.second_cateid" placeholder="--请选择-">
          <!-- 遍历二级分类 -->
          <el-option
            v-for="item in second_cate"
            :key="item.id"
            :label="item.catename"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="商品名称" :label-width="formLabelWidth">
        <el-input v-model="form.goodsname"></el-input>
      </el-form-item>
      <el-form-item label="价格" :label-width="formLabelWidth">
        <el-input v-model="form.price"></el-input>
      </el-form-item>
      <el-form-item label="市场价格" :label-width="formLabelWidth">
        <el-input v-model="form.market_price"></el-input>
      </el-form-item>
      <el-form-item label="图片" :label-width="formLabelWidth">
        <el-upload
          class="avatar-uploader"
          action="#"
          :show-file-list="false"
          :on-change="changeImg"
        >
          <img v-if="imageUrl" :src="imageUrl" class="avatar" />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>
      <el-form-item label="商品规格" :label-width="formLabelWidth">
        <!-- <el-input v-model="form.specsid"></el-input>
         -->
        <el-select
          v-model="form.specsid"
          placeholder="--请选择--"
          @change="changeSpecs"
        >
          <!-- 遍历商品规格 -->
          <el-option
            v-for="item in specs"
            :key="item.id"
            :label="item.specsname"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="规格属性" :label-width="formLabelWidth">
        <el-select v-model="form.specsattr" placeholder="--请选择--" multiple>
          <el-option
            v-for="item in second_attrs"
            :key="item"
            :label="item"
            :value="item"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="是否新品" :label-width="formLabelWidth">
        <el-radio v-model="form.isnew" :label="1">是</el-radio>
        <el-radio v-model="form.isnew" :label="2">否</el-radio>
      </el-form-item>
      <el-form-item label="是否热卖" :label-width="formLabelWidth">
        <el-radio v-model="form.ishot" :label="1">是</el-radio>
        <el-radio v-model="form.ishot" :label="2">否</el-radio>
      </el-form-item>
      <el-form-item label="状态" :label-width="formLabelWidth">
        <el-switch
          v-model="form.status"
          active-color="blue"
          inactive-color="grey"
          :active-value="1"
          :inactive-value="2"
        ></el-switch>
      </el-form-item>
      <el-form-item label="商品描述" :label-width="formLabelWidth">
        <div id="div1"></div>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="confirm" v-if="info.isAdd"
        >确 定</el-button
      >
      <el-button type="primary" @click="update" v-else>修 改</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import E from "wangeditor";
import { addGoods, goodsManagerOne } from "../../../request/api";
import { successAlert } from "../../../utils/alert";
import { mapActions, mapGetters } from "vuex";
export default {
  props: ["info"],
  data() {
    return {
      form: {
        first_cateid: "",
        second_cateid: "",
        goodsname: "",
        price: "",
        market_price: "",
        img: "",
        description: "",
        specsid: "",
        specsattr: [],
        isnew: 1,
        ishot: 1,
        status: 1,
      },
      imageUrl: "",
      formLabelWidth: "70px",
      second_cate: [], //二级分类列表
      second_attrs: [], //规格属性列表
      editor: "", //富文本内容
    };
  },
  computed: {
    ...mapGetters({
      cates: "cate/cate",
      specs: "goodsSpec/specs",
    }),
  },
  methods: {
    ...mapActions({
      cateListActions: "cate/cateListActions",
      specListActions: "goodsSpec/specListActions",
      countActions: "goodsManager/countActions",
      goodsActions: "goodsManager/goodsActions",
      goodsUpdateActions: "goodsManager/goodsUpdateActions",
    }),

    cancel() {
      this.info.isShow = false;
      this.form = {
        first_cateid: "",
        second_cateid: "",
        goodsname: "",
        price: "",
        market_price: "",
        img: "",
        description: "",
        specsid: "",
        specsattr: [],
        isnew: 1,
        ishot: 1,
        status: 1,
      };
      this.imageUrl = "";
      this.editor.txt.html(this.form.description);
      (this.second_cate = []), (this.second_attrs = []);
    },
    confirm() {
      this.form.description = this.editor.txt.html();
      addGoods(this.form).then((res) => {
        if (res.code === 200) {
          successAlert(res.msg);
          this.goodsActions();
          this.cancel();
        }
      });
    },
    // 级联
    changeCate() {
      // 清空二级分类的值
      this.form.second_cateid = "";
      // 1.获取下标
      let index = this.cates.findIndex(
        (item) => item.id === this.form.first_cateid
      );
      // 2.根据下标获取children下面的二级分类
      this.second_cate = this.cates[index].children;
    },
    changeSpecs() {
      //清空二级分类的值
      this.form.specsattr = [];
      // 1.获取下标
      let index = this.specs.findIndex((item) => item.id === this.form.specsid);
      // 2.根据下标获取attrs下面的规格属性
      this.second_attrs = this.specs[index].attrs;
    },
    changeImg(file) {
      // 1.处理文件大小
      if (file.size > 2 * 1024 * 1024) {
        warningAlert("上传文件不能超过2M");
        return;
      }
      // 2.处理文件格式 图片:.jpg,.png,.jpeg,.gif
      let ext = [".jpg", ".png", ".jpeg", ".gif"];
      let extName = file.name.slice(file.name.lastIndexOf("."));
      if (!ext.some((item) => item === extName)) {
        waringAlert("上传文件格式不正确");
        return;
      }

      // 3.生成本地url地址，赋值给imageUrl
      this.imageUrl = URL.createObjectURL(file.raw);
      // 4.将文件信息赋值给form.img
      this.form.img = file.raw; 
    },
    opened() {
      if (!this.editor) {
        this.editor = new E("#div1");
        this.editor.create();
      }
    },
    oneDetail(id) {
      goodsManagerOne({ id }).then((res) => {
        if (res.code === 200) {
          if (!this.editor) {
            this.editor = new E("#div1");
            this.editor.create();
          }
          this.form = res.list;
          this.form.id = id;
          
          // this.second_attrs=this.form.specsattr;
          let index = this.cates.findIndex(
            (item) => item.id === this.form.first_cateid
          );
          // 2.根据下标获取children下面的二级分类
          this.second_cate = this.cates[index].children;
          // 2.根据下标获取children下面的二级分类
          // this.second_cate = this.form.sec;
          // 1.获取下标
          let index2 = this.specs.findIndex(
            (item) => item.id === this.form.specsid
          );
          // 2.根据下标获取attrs下面的规格属性
          this.second_attrs = this.specs[index2].attrs;
          this.imageUrl = this.$pregImg + res.list.img;
          console.log( this.second_attrs);
          this.editor.txt.html(this.form.description);
        }
      });
    },
    update() { 
      // this.form.attrs = this.specsattr.map((item) => item.value).join(",");
      this.form.description = this.editor.txt.html();
      this.goodsUpdateActions(this.form); 
      this.countActions();
      this.goodsActions();
      this.cancel();
    },
  },
  mounted() {
    this.cateListActions();
    this.specListActions();
  },
};
</script>

<style scoped>
/* >>> 穿透,   穿过scoped局部作用于进入element-ui的样式 */
.avatar-uploader >>> .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader >>> .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>